// pages/center/balance/account/account.js
var verify = require('../../../../utils/verify-token.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasUser: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo = app.globalData.userInfo;
    var pic = userInfo.avatarUrl;
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      userInfo: app.globalData.userInfo
    })

    var openid = app.globalData.openid;
		console.log("openid="+openid);
    var baseUrl = app.globalData.baseUrl;
    var token = app.globalData.token;
    var that = this;
    wx.request({
      url: 'https://web.api.sdaascloud.com/web/personAcc/getAccountInfo',
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
           console.log("openid = ",res.data.data.openid)
          if (res.data.data.openid != null&&res.data.data.openid!='') {					
            that.setData({
              hasUser: true,
              pic: res.data.data.pic
            })
          } else {
            that.setData({
              pic: pic
            })
          }
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })

  },

  modifyWx: function () {
    var hasUser = !this.data.hasUser
    var pic = this.data.userInfo.avatarUrl
    var openid = app.globalData.openid;
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this
		//console.log(openid)
		//console.log(hasUser)
    if (hasUser){
      wx.request({
        url: 'https://web.api.sdaascloud.com/web/personAcc/addAccountInfo?pic=' + pic + '&openid=' + openid,
        method: "post",
        header: {
          'AUTHORIZATION': token
        },
        success(res) {
          // console.log("add = ", res)
          if (res.data.code == 60006) {
            verify.clearAll()
          } else if (res.data.code == 200) {
            wx.showToast({
              title: '操作成功',
            })
          } else {
            wx.showModal({
              title: '操作失败',
              content: res.data.message,
            })
          }
        }
      })
    }else{
      wx.request({
        url: 'https://web.api.sdaascloud.com/web/personAcc/deleteAccountInfo',
        method: "post",
        header: {
          'AUTHORIZATION': token
        },
        success(res) {
          // console.log("delete = ", res)
          if (res.data.code == 60006) {
            verify.clearAll()
          } else if (res.data.code == 200) {
            wx.showToast({
              title: '操作成功',
            })
          } else {
            wx.showModal({
              title: '操作失败',
              content: res.data.message,
            })
          }
        }
      })
    }

    this.setData({
      hasUser: hasUser,
      pic:pic
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})